package com.junci.mtest.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.junci.mtest.MyApplication;
import com.junci.mtest.R;


/**
 * 单例Toast
 */
public class ToastUtil {

    private static Toast makeText = null;

    /**
     * 这是个单例Toast，只显示最新的Toast，会覆盖旧的Toast.没有登录的时候，需要提示用户用这个Toast
     *
     * @param context 上下文
     * @param content Toast内容
     */
    public static void noLoginShow(Context context, String content) {
        if (context == null) {
            return;
        }
        if (makeText != null) {
            makeText.setText(content);
            makeText.show();
        } else {
            makeText = Toast.makeText(context.getApplicationContext(), content, Toast.LENGTH_SHORT);
            makeText.show();
        }
    }

    /**
     * 这是个单例Toast，只显示最新的Toast，会覆盖旧的Toast
     *
     * @param context 上下文
     * @param content Toast内容
     */
    public static void show(Context context, String content) {
        if (!TextUtils.isEmpty(content)) {
            show(context, content, true);
        }
    }

    /**
     * 这是个单例Toast，只显示最新的Toast，会覆盖旧的Toast
     *
     * @param context 上下文
     * @param content Toast内容
     */
    public static void show(Context context, String content, boolean isLong) {
        if (context == null) {
            if (makeText != null) {
                makeText.setText(content);
                TextView tv =  makeText.getView().findViewById(R.id.tv_text);
                tv.setText(content);
                makeText.show();
            } else {
                if (isLong) {
//                    makeText = Toast.makeText(MyApplication.application, content, Toast.LENGTH_LONG);

                    View layout = View.inflate(context,R.layout.toast_layout,null);

                    makeText = Toast.makeText(context, content, Toast.LENGTH_LONG);
                    makeText.setView(layout);

                    makeText.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    TextView tv =  makeText.getView().findViewById(R.id.tv_text);
                    tv.setText(content);

                } else {
                    makeText = Toast.makeText(MyApplication.application, content, Toast.LENGTH_SHORT);
                }
                makeText.show();
            }
        }else {
//            if (makeText != null) {
//                makeText.setText(content);
//                TextView tv =  makeText.getView().findViewById(R.id.tv_text);
//                tv.setText(content);
//                makeText.show();
//            } else {
                if (isLong) {
//                    LayoutInflater inflater = View.inflate();
                    View layout = View.inflate(context,R.layout.toast_layout,null);

                    makeText = Toast.makeText(context, content, Toast.LENGTH_LONG);
                    makeText.setView(layout);

                    makeText.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    TextView tv =  makeText.getView().findViewById(R.id.tv_text);
                    tv.setText(content);
//                    makeText.setDuration(Toast.LENGTH_LONG);
                } else {
                    makeText = Toast.makeText(context, content, Toast.LENGTH_SHORT);
                }
                makeText.show();
//            }
        }
//        toastOnUIThread(context,content,isLong);
    }


    /**
     * 这是个单例Toast，只显示最新的Toast，会覆盖旧的Toast，这个是显示得比较久的
     *
     * @param context 上下文
     * @param content Toast内容
     */
    public static void showLong(Context context, String content) {
        show(context, content, true);
    }

    public static void showUnLink(Context context) {
        String content = "网络不给力";
        show(context, content);
    }

    public static void cancel() {
        if (makeText != null) {
            makeText.cancel();
        }
    }



    /*************Toast 在 UI-Thread**************/
    public static void toastOnUIThread(final Context context, final String string, final boolean isLong) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                show(context, string, isLong);
            }
        });
    }

    public static void toastOnUIThread(String string) {
        toastOnUIThread(MyApplication.application.getApplicationContext(), string, false);
    }

    public static void toastOnUIThreadLong(String string) {
        toastOnUIThread(MyApplication.application.getApplicationContext(), string, true);
    }

    public static void showToast(String string) {

        show(MyApplication.application.getApplicationContext(), string);
    }




}